<template>
  <el-drawer v-model="showDrawer" :title="title" :size="size" :close-on-click-modal="closeOnClickModal" :destroy-on-close="destroyOnClose">
    <div class="formDrawer">
      <div class="body">
        <slot></slot>
      </div>
      <div class="actions">
        <el-button type="primary" :loading="loading" @click="submit">{{ comfirmText }}</el-button>
        <el-button type="default" @click="close">取消</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '没有指定标题'
  },
  size: {
    type: String,
    default: '45%'
  },
  destroyOnClose: {
    type: Boolean,
    default: true
  },
  comfirmText: {
    type: String,
    default: '确定'
  },
  closeOnClickModal: {
    type: Boolean,
    default: true
  }
})

// <!--<editor-fold desc="变量">-->
const showDrawer = ref(false)
const loading = ref(false)
// <!--</editor-fold>-->

const emits = defineEmits(['submit'])

// #region 方法
const open = () => (showDrawer.value = true)
const close = () => (showDrawer.value = false)
const submit = () => emits('submit')
const showLoading = () => (loading.value = true)
const hideLoading = () => (loading.value = false)
// #endregion

// #region 暴露给父组件
defineExpose({
  open,
  close,
  showLoading,
  hideLoading
})
// #endregion
</script>
<style scoped>
.formDrawer {
  width: 100%;
  height: 100%;
  position: relative;
  @apply flex flex-col;
}

.formDrawer .body {
  flex: 1;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  overflow-y: auto;
}

.formDrawer .actions {
  height: 50px;
  @apply mt-auto flex items-center;
}
</style>
